package com.example.tcp2504;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.json.JSONObject;

public class learnService extends Service {
    private final String TAG = "TCPService";
    private final MyBinder binder = new MyBinder();
    public static final String ACTION = "com.example.TCP_ACTION";
    public static final String MESSAGE_KEY = "message";

    // 定义一个内部类，继承自 Binder
    public class MyBinder extends Binder {
        // 允许客户端调用 Service 的方法
        public learnService getService() {
            return learnService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "Service onBind");
        return binder; // 返回 Binder 对象
    }

    @Override
    public void onCreate() {
        /**
         * 启动动服务：
         * 如果 Service 尚未运行，startService 方法会创建并启动 Service。
         * 如果 Service 已经运行，则 startService 方法不会重新创建 Service，但会调用其 onStartCommand 方法。
         */
        super.onCreate();
        Log.d(TAG, "Service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "Service onStartCommand");
        // 模拟后台任务
        new Thread(() -> {
            /**
             学习和activity进行intent通信的代码
             */
            String name = null;
            int age = 0;
            try {
                // 从 Intent 中获取 JSON 对象
                if (intent != null && intent.hasExtra("json_object")) {
                    String jsonStr = intent.getStringExtra("json_object");
                    try {
                        JSONObject jsonObject = new JSONObject(jsonStr);
                        Log.d(TAG, "Received JSON Object: " + jsonObject.toString());

                        // 在这里处理 JSON 对象
                        name = jsonObject.optString("name");
                        age = jsonObject.optInt("age");
                        Log.d(TAG, "Name: " + name + ", Age: " + age);


                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e(TAG, "Error parsing JSON: " + e.getMessage());
                    }
                }
                Thread.sleep(3000);
                Log.d(TAG, "Task completed in Service");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            /**
             * 学习使用广播进行通信的代码
             */
            // 发送广播通知 Activity
            Intent broadcastIntent = new Intent(ACTION);
            broadcastIntent.putExtra(MESSAGE_KEY, "Task completed with Name: " + name + ", Age: " + age);
            LocalBroadcastManager.getInstance(learnService.this).sendBroadcast(broadcastIntent);

        }).start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service onDestroy");
    }

    // 定义一个方法供 Activity 调用
    public void doSomething() {
        Log.d(TAG, "Service doSomething called");
    }
}